﻿using NDK.Module.StationLogic;
using NDK.Motion.Controls;
using NDK.Motion.StationLogic;
using NDK.Motion.ViewModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace NDK.Frm
{
    public partial class Frm_StationDebug1 : Form
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("Frm_StationDebug");
        List<ucDebug_CT> ucDebug_CTList = new List<ucDebug_CT>();

        #region 构造函数
        private Frm_StationDebug1()
        {
            InitializeComponent();
            //CheckForIllegalCrossThreadCalls = false;

        }
        private static Frm_StationDebug1 _instance;
        private static readonly object singletonLock = new object();
        public static Frm_StationDebug1 Instance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null || _instance.IsDisposed)
                        {
                            _instance = new Frm_StationDebug1();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion
        public List<StepViewModel> steps = new List<StepViewModel>();
        private void Frm_StationDebug_Load(object sender, EventArgs e)
        {
            if (StationManager.StationList == null || StationManager.StationList.Count == 0)
            {
                return;
            }
            foreach (var station in StationManager.StationList)
            {
                //添加tab页
                TabPage tp = new TabPage();
                if (station.Name != string.Empty)
                {
                    tp.Text = station.Name;
                }
                else
                {
                    tp.Text = station.GetType().Name;
                }
                //添加TableLayoutPanel在tp页面
                TableLayoutPanel tlp = new TableLayoutPanel();
                tlp.ColumnCount = 2;
                tlp.RowCount = 1;
                tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
                tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
                tlp.Dock = DockStyle.Fill;
                tp.Controls.Add(tlp);
                //添加CT监控控件到TableLayoutPanel页面(0行0列)
                ucDebug_CT ucDebug_CTTemp = new ucDebug_CT();
                ucDebug_CTTemp.Name = station.GetType().Name;
                ucDebug_CTTemp.Title = tp.Text;
                tlp.Controls.Add(ucDebug_CTTemp, 0, 0);
                ucDebug_CTList.Add(ucDebug_CTTemp);
                //设置监控控件的属性事件
                ucDebug_CTTemp.Dock = DockStyle.Fill;
                var ns = station.GetType().FullName;
                Type t = Type.GetType(ns + "+StationStep");
                ucDebug_CTTemp.Steps = ForEnum(t);
                station.PropertyChanged += Station_PropertyChanged;
                //添加变量监控控件到TableLayoutPanel页面(0行1列)
                ucDebug_Var ucDebug_VarTemp = new ucDebug_Var();
                ucDebug_VarTemp.MonitoredObject = station;
                ucDebug_VarTemp.Name = station.GetType().Name;
                ucDebug_VarTemp.Title = tp.Text;
                tlp.Controls.Add(ucDebug_VarTemp, 1, 0);
                ucDebug_VarTemp.Dock = DockStyle.Fill;
                tabControl1.Controls.Add(tp);

            }
        }

        private void Station_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            foreach (StationLogicBase station in StationManager.StationList)
            {
                if (((StationLogicBase)sender).Equals(station))
                {
                    string name = station.GetType().Name;
                    ucDebug_CT ucDebug_CT = ucDebug_CTList.FirstOrDefault(u => u.Name == name);
                    if (ucDebug_CT != null)
                    {
                        ucDebug_CT.CruStep = e.PropertyName;
                    }
                }
            }
        }

        /// <summary>
        /// 获取步骤枚举变量中的值，和描述信息，初始化CT列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        private List<StepViewModel> ForEnum(Type type)
        {
            List<StepViewModel> steps = new List<StepViewModel>();

            Array arrays = Enum.GetValues(type);
            for (int i = 0; i < arrays.LongLength; i++)
            {
                var value = arrays.GetValue(i);
                FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
                object[] attribArray = fieldInfo.GetCustomAttributes(false);
                string description = "";
                if (attribArray.Count() > 0)
                {
                    DescriptionAttribute attrib = (DescriptionAttribute)attribArray[0];
                    description = attrib.Description;
                }

                StepViewModel step = new StepViewModel();
                step.StepName = value.ToString();
                step.StepDescription = description;
                steps.Add(step);
            }

            return steps;
        }

        private void Frm_StationDebug_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
    }
}
